package buff;

import org.apache.tomcat.jni.Pool;
import org.apache.tomcat.jni.Shm;
import shm.AbstractCommon;

import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * License: Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html)
 * <p/>
 * Author: Ashwin Jayaprakash
 * <p/>
 * Email: ashwin.jayaprakash@gmail.com
 * <p/>
 * Web: http://www.ashwinjayaprakash.com
 */
public class AprBufferTest extends JavaBufferTest {
    public AprBufferTest() {
        AbstractCommon.sout("");
    }

    protected void testShm() throws Exception {
        final long poolHandle = Pool.create(0);
        final LinkedList<Long> shmHandles = new LinkedList<Long>();

        BufferMaker bufferMaker = new BufferMaker() {
            int counter;

            @Override
            public ByteBuffer make(int size) throws Exception {
                String shmName = getTestType() + "_" + getClass().getSimpleName() + "_" + counter++;

                try {
                    Shm.remove(shmName, poolHandle);
                }
                catch (Exception e) {
                }

                long shmHandle = 0;
                try {
                    shmHandle = Shm.attach(shmName, poolHandle);
                }
                catch (org.apache.tomcat.jni.Error error) {
                    shmHandle = Shm.create(size, shmName, poolHandle);
                }
                shmHandles.add(shmHandle);

                return Shm.buffer(shmHandle);
            }
        };

        try {
            testActual(bufferMaker);
        }
        finally {
            for (Iterator<Long> iterator = shmHandles.iterator(); iterator.hasNext(); ) {
                Long shmHandle = iterator.next();

                try {
                    Shm.destroy(shmHandle);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }

                iterator.remove();
            }

            Pool.destroy(poolHandle);
        }
    }

    @Override
    protected boolean runTest(String[] args, int loopCount) throws Exception {
        if (args[0].equalsIgnoreCase("shm")) {
            setTestType(args[0]);
            int blockSizeBytes = Integer.parseInt(args[1]);
            int numBlocks = Integer.parseInt(args[2]);
            setBlockSpec(blockSizeBytes, numBlocks);

            beforeTestLoop();
            for (int i = 0; i < loopCount; i++) {
                testShm();
            }
            afterTestLoop();
        }
        else {
            boolean success = super.runTest(args, loopCount);

            if (!success) {
                System.err.println("    shm <block_size_bytes> <num_blocks>");
            }

            return success;
        }

        return true;
    }

    public static void main(String[] args) throws Exception {
        new AprBufferTest().runTest(args, 24);
    }
}
